﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics;
using System.Net;
using System.Web.UI;
using System.Web.UI.WebControls;
using RestSharp;
using RestSharp.Authenticators;
using Xunit;
using Newtonsoft.Json;
using XingNet.Web.Model;
using XingNet.Web.Communication;
using XingNet.Web.Communication.Base;
using XingNet.Web.Communication.Requests;
using XingNet.Web.Communication.Requests.v1_Users;
namespace MvcXingAPI.Controllers
{
  public class HomeController : Controller
  {
    public ActionResult Index()
    {
    
        if (_TokenHandler == null)
        {
          _TokenHandler = new XingNetTokenHandler(ConsumerKey, ConsumerSecret);
        }
        if (Request.Cookies["Localhost"] != null)
        {
          System.Web.HttpCookie Keks = Request.Cookies["Localhost"];
          _TokenHandler.Access_Token =  Keks.Values["oauth_token"];
          _TokenHandler.Access_Token_Secret =  Keks.Values["oauth_token_secret"];
          _TokenHandler.Verifier = Keks.Values["verifier"];
        }

        v1_Users_Request UserRequest = new v1_Users_Request(_TokenHandler);
        XingNet.Web.Model.v1_Users.UserProfile Profile;
        Profile = UserRequest.GetProfileForLoggedInUser().UserProfiles[0];

        return View(Profile);
    }

    public ActionResult Contact()
    {
      ViewBag.Message = "Your contact page.";
      return View();
    }

   public static XingNetTokenHandler _TokenHandler;
   static string ConsumerKey = "cade0141b023e0e99a1d";
   static string ConsumerSecret = "ff9bde141ffbe878ccd9c801d7a94dfe7c8753bd";
   static Uri CallbackUrl;
    public ActionResult Login()
    {
      //Create the TokenHandler
      _TokenHandler = new XingNetTokenHandler(ConsumerKey, ConsumerSecret);

      CallbackUrl = new Uri(Request.Url.Scheme + "://" + Request.Url.Authority + "/Home/LoginCallBack");

      var client = XingBaseClient.GetClient(_TokenHandler, CallbackUrl.ToString(),XingBaseClient.RequestMode.RequestToken);
      var request = new RestRequest("v1/request_token", Method.POST);
      var response = client.Execute(request);


      var qs = HttpUtility.ParseQueryString(response.Content);
      _TokenHandler.Access_Token = qs["oauth_token"];
      _TokenHandler.Access_Token_Secret = qs["oauth_token_secret"];

      request = new RestRequest("/v1/authorize");
      request.AddParameter("oauth_token", _TokenHandler.Access_Token);
      var url = client.BuildUri(request).ToString();
      Response.Redirect(url);

      return null;
    }
    public ActionResult LoginCallBack()
    {
      _TokenHandler.Access_Token = Request["oauth_token"];
      _TokenHandler.Verifier = Request["oauth_verifier"];
      var request = new RestRequest("/v1/access_token", Method.POST);
      request.AddParameter("oauth_token", _TokenHandler.Access_Token);
      request.AddParameter("oauth_verifier", _TokenHandler.Verifier);

      var client = XingBaseClient.GetClient(_TokenHandler, CallbackUrl.ToString(), XingBaseClient.RequestMode.AccessToken);
      var response = client.Execute(request);


      //Change tokens to the correct Access Tokens
      var qs = HttpUtility.ParseQueryString(response.Content);
      _TokenHandler.Access_Token = qs["oauth_token"];
      _TokenHandler.Access_Token_Secret = qs["oauth_token_secret"];
      _TokenHandler.UserID = qs["User_ID"];
 
  
      System.Web.HttpCookie Keks = new System.Web.HttpCookie("Localhost");
      Keks.Name = "localhost";
      Keks.Domain = "http://localhost:60130/";
      Keks.Expires = System.DateTime.Now.AddYears(2);                                                      
      Keks.Values["oauth_token"] = _TokenHandler.Access_Token;
      Keks.Values["oauth_token_secret"] = _TokenHandler.Access_Token_Secret;
      Keks.Values["verifier"] = _TokenHandler.Verifier;
      Response.Cookies.Add(Keks);




      //Now we have the Tokens and can use the API
      return RedirectToAction("Index", "Home");
    }

    public ActionResult About()
    {
      if (_TokenHandler == null)
      {
        _TokenHandler = new XingNetTokenHandler(ConsumerKey, ConsumerSecret);
      }
      if (Request.Cookies["Localhost"] != null)
      {
        System.Web.HttpCookie Keks = Request.Cookies["Localhost"];
        _TokenHandler.Access_Token = Keks.Values["oauth_token"]; //"47769ece87536d359a28"; // 
        _TokenHandler.Access_Token_Secret = Keks.Values["oauth_token_secret"]; //"1b655659d1f58abd4aec";
        _TokenHandler.Verifier = Keks.Values["verifier"];
      }

      v1_Users_Request UserRequest = new v1_Users_Request(_TokenHandler);
      XingNet.Web.Model.v1_Users.UserProfile Profile;
      Profile = UserRequest.GetProfileForLoggedInUser().UserProfiles[0];

      return View(Response);
    }
  }
}
